$(function () {
  //1.获取文章分类信息渲染到页面，封装成函数。后面增删改要用
  let layer = layui.layer
  initArtCateList()
  function initArtCateList() {
    axios({
      method: 'get',
      url: '/my/article/cates',
    }).then(res => {
      if (res.data.status !== 0) {
        return layer.msg('res.data.message')
      }
      //渲染
      let arr = []
      res.data.data.forEach(ele => {
        arr.push(`
              <tr>
                <td>${ele.Id}</td>
                <td>${ele.name}</td>
                <td>${ele.alias}</td>
                <td>
                  <button data-id="${ele.Id}" class="btn-edit layui-btn layui-btn-xs">修改</button>
                  <button data-id="${ele.Id}" class="del-edit layui-btn layui-btn-xs layui-btn-danger">删除</button>
                </td>
              </tr>
        `)
        $('tbody').empty().html(arr.join(''))
      })
    })
  }

  //2.点击按钮弹出弹出层
  let indexAdd = 0
  $('#addBtn').on('click', function () {
    //弹出框是layui官网调试后，cv过来的
    indexAdd = layer.open({
      type: 1,
      title: '添加文章类别',
      area: ['500px', '260px'],
      content: `
                <form id="addForm" class="layui-form" action="">
                <!-- 分类名称 -->
                <div class="layui-form-item">
                  <label class="layui-form-label">分类名称</label>
                  <div class="layui-input-block">
                    <input type="text" name="name" required lay-verify="required" placeholder="请输入分类名称" autocomplete="off" class="layui-input" />
                  </div>
                </div>
                <!-- 分类别称 -->
                <div class="layui-form-item">
                  <label class="layui-form-label">分类别称</label>
                  <div class="layui-input-block">
                    <input type="text" name="alias" required lay-verify="required" placeholder="请输入分类别称" autocomplete="off" class="layui-input" />
                  </div>
                </div>
                <!-- 按钮 -->
                <div class="layui-form-item">
                <div class="layui-input-block">
                  <button class="layui-btn" lay-submit lay-filter="formDemo">立即提交</button>
                  <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                </div>
                </div>
                </form>
                `,
    })
  })

  //3.添加文章分类    动态的添加需要事件委托
  $('body').on('submit', '#addForm', function (e) {
    e.preventDefault()
    //发送axios
    axios({
      method: 'post',
      url: '/my/article/addcates',
      data: $(this).serialize(),
    }).then(res => {
      if (res.data.status !== 0) {
        return layer.msg(res.data.message)
      }
      //成功提示
      layer.msg('恭喜您，添加文章分类成功!')
      initArtCateList()
      //关闭对话框
      layer.close(indexAdd)
    })
  })

  //4.修改弹出框
  let indexEdit = 0
  $('tbody').on('click', '.btn-edit', function () {
    indexEdit = layer.open({
      type: 1,
      title: '修改文章类别',
      area: ['500px', '260px'],
      content: `
                <form id="editForm" class="layui-form" lay-filter="editForm">
                <!-- 分类名称 -->
                <input type="hidden" name="Id">
                <!-- 分类名称 -->
                <div class="layui-form-item">
                  <label class="layui-form-label">分类名称</label>
                  <div class="layui-input-block">
                    <input type="text" name="name" required lay-verify="required" placeholder="请输入分类名称" autocomplete="off" class="layui-input" />
                  </div>
                </div>
                <!-- 分类别称 -->
                <div class="layui-form-item">
                  <label class="layui-form-label">分类别称</label>
                  <div class="layui-input-block">
                    <input type="text" name="alias" required lay-verify="required" placeholder="请输入分类别称" autocomplete="off" class="layui-input" />
                  </div>
                </div>
                <!-- 按钮 -->
                <div class="layui-form-item">
                <div class="layui-input-block">
                  <button class="layui-btn" lay-submit lay-filter="formDemo">确认修改</button>
                  <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                </div>
                </div>
                </form>
                `,
    })
    //获取id
    let id = $(this).attr('data-id')
    //axios
    axios({
      method: 'get',
      //url参数，路由参数，动态参数
      //    /   必须保留    :   必须删除，    id是动态变化的
      url: '/my/article/cates/' + id,
    }).then(res => {
      if (res.data.status !== 0) {
        return layer.msg(res.data.message)
      }
      //成功
      layui.form.val('editForm', res.data.data)
    })
  })

  //5.点击修改按钮，修改文章分。
  //基于添加逻辑修改代码
  $('body').on('submit', '#editForm', function (e) {
    e.preventDefault()
    //修改分类
    axios({
      method: 'post',
      url: '/my/article/updatecate',
      data: $(this).serialize(),
    }).then(res => {
      if (res.data.status !== 0) {
        return layer.msg(res.data.message)
      }
      //修改没有提示

      //成功提示
      layer.msg('恭喜您，修改文章分类成功!')
      initArtCateList()
      //关闭对话框
      layer.close(indexEdit)
    })
  })

  //6.删除
  $('tbody').on('click', '.del-edit', function () {
    //获取自定义属性 id
    let id = $(this).attr('data-id')
    layer.confirm('是否确认删除？', { icon: 3, title: '提示' }, function (index) {
      //axios
      axios({
        method: 'get',
        url: '/my/article/deletecate/' + id,
      }).then(res => {
        if (res.data.status) {
          return layer.msg(res.data.message)
        }
        //成功
        //成功提示
        layer.msg('恭喜您，删除文章分类成功!')
        initArtCateList()
      })
      //关闭对话框
      layer.close(index)
    })
  })
})
